<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>角色管理</title>
<%@ include file="/common/head.jsp"%>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>角色管理</span>
        </div>
        <div id="toolbar" style="float: right"></div>
    </div>
    <div class="page-title-border"></div>
</div>

<div class="search-field">
    <span class="field-cont">
        <span class="field-cont-title">角色名称</span>
        <input type="text" class="summer-input" id="roleName">
    </span>
    
    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>

<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <col width="30%">
	    <col width="30%">
	    <col width="40%">
	    <thead>
		    <tr>
		        <th>角色名称</th>
		        <th>创建时间</th>
		        <th>操作</th>
		    </tr>
	    </thead>
    </table>
</div>

<script>
	var ROLE_CONST_URL = {
	        LIST : basePath + '/sys/role/list',
	        ADD : basePath + "/sys/role/initAdd",
	        EDIT : basePath + "/sys/role/initEdit",
	        DEL : basePath + "/sys/role/delete",
	        AUTHORIZE : basePath + "/sys/role/initAuthorize"
	};
	
	var permission = CommonUtil.getPermission('role');
	
	
	$(function() {
		
		// 新建toobar
		$.funcbar({
			id: 'toolbar',
			type: 'toolbar',
			data: permission,
			permit: ['role:initAdd'],
		});
		
		// 创建表格
		createTable();
		
		// 查询按钮
        $('#search').on("click", function() {
            reloadTable();
        });
		
        // 重置按钮
        $('#reset').on("click", function() {
            $('#roleName').val("");
            reloadTable();
        });
		
		// 新建
		$('#toolbar').on('click', 'a[data-permit="role:initAdd"]', function() {
			location.href = ROLE_CONST_URL.ADD;  	  	
		});
		
		// 删除
		$('#t-wrap').on('click', 'a[data-permit="role:delete"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要删除该角色吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : ROLE_CONST_URL.DEL + "?ids="+id,
			                onLoadSuccess : function() {
			                	$.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
		
		// 授权
		$('#t-wrap').on('click', 'a[data-permit="role:initAuthorize"]', function() {
			var $this = $(this),
            id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			location.href = ROLE_CONST_URL.AUTHORIZE+'?id='+id;	
		
		});
		
		// 编辑
		$('#t-wrap').on('click', 'a[data-permit="role:initEdit"]', function() {
            var $this = $(this),
            id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
            location.href = ROLE_CONST_URL.EDIT+'?id='+id; 
        
        });				
		
	});
    
	// 重新加载表格
	function reloadTable() {
		$('#dataList').datagrid('reload',{
            ajax: {
                param: {"name": $("#roleName").val()}
            }
        });
	}
	
	// 初始化表格
	function createTable() {
		$('#dataList').datagrid({
            ajax: {
                url: ROLE_CONST_URL.LIST,
                param: {"name": $("#roleName").val()}
            },
            columns: [
                {
                    field: 'id',
                    title: 'ID',
                    hidden: true
                },
                {
                    field: 'name',
                },
                {
                    field: 'createTime'
                },
                {
                    render: function (rowData) { 
                         return  $.funcbar({
                            data: permission,    //请求的数据
                            type:'text',    //类型
                            permit: ['role:initEdit', 'role:initAuthorize', 'role:delete'] //需要的权限按钮
                        });
                    }
                }
            ]

        });		
	}	

</script>
</body>
</html>